$(function(){
	/**
	 * 给关注按钮添加事件
	 */
	Follow.followButtonEvent();
});
var Follow = {
		/**
		 * 给关注按钮添加事件
		 */
		followButtonEvent : function(){
			Follow.followEvent("a[id^='pf-']","a[id^='qf-']","a[id^='tf-']");
		},
		/**
		 * 给POpup关注按钮添加事件
		 */
		popupFollowButtonEvent : function(){
			Follow.followEvent("#bubble a[id^='pf-']",null,"#bubble a[id^='tf-']");
		},
		/**
		 * 添加关注事件
		 * @param userReg
		 * @param questionReg
		 * @param tagReg
		 */
		followEvent : function(userReg,questionReg,tagReg){
			
			if(tagReg && tagReg.length > 1){
				
				$(tagReg).each(function(){
					var $_this = $(this);
					var _this = this;
					var btn_class = $_this.parent().attr("class");
					if(btn_class && btn_class.indexOf("b_add_attention") >= 0){//按钮形式
						$_this.parent().unbind("click");
						$_this.parent().bind("click",function(){
							if(DZQCheckForUser())
								return;
							
							var id = $_this.attr("id");
							var ids = id.split("-");
							var text = $_this.text();
							var tag = (ids.length == 3) ? (ids[1] + "-" +ids[2]):ids[1];
							if(text.indexOf("取消") >= 0 ){
								Follow.followOrUnFollowTag(tag, -1,_this);
							}
							else{
								Follow.followOrUnFollowTag(tag, 1,_this);
							}
						});
					}else{
						$_this.unbind("click");
						$_this.bind("click",function(){
							if(DZQCheckForUser())
								return;
							
							var id = $_this.attr("id");
							var ids = id.split("-");
							var text = $_this.text();
							var tag = (ids.length == 3) ? (ids[1] + "-" +ids[2]):ids[1];
							if(text.indexOf("取消") >= 0 ){
								Follow.followOrUnFollowTag(tag, -1,this);
							}
							else{
								Follow.followOrUnFollowTag(tag, 1,this);
							}
						});
					}
				});
				/*
				$(tagReg).bind("click",function(){
					if(DZQCheckForUser())
						return;
					
					var id = $(this).attr("id");
					var ids = id.split("-");
					var text = $(this).text();
					if(text.indexOf("取消") >= 0 ){
						Follow.followOrUnFollowTag(ids[1], -1,this);
					}
					else{
						Follow.followOrUnFollowTag(ids[1], 1,this);
					}
				});
				*/
			}
			
			if(userReg && userReg.length > 1){
				$(userReg).each(function(){
					var $_this = $(this);
					var _this = this;
					var btn_class = $_this.parent().attr("class");
					if(btn_class && btn_class.indexOf("b_add_attention") >= 0){//按钮形式
						$_this.parent().unbind("click");
						$_this.parent().bind("click",function(){
							if(DZQCheckForUser())
								return;
							
							var id = $_this.attr("id");
							var ids = id.split("-");
							var text = $_this.text();
							if(text.indexOf("取消") >= 0 ){
								Follow.followOrUnFollowUser(ids[1], -1,_this);
							}
							else{
								Follow.followOrUnFollowUser(ids[1], 1,_this);
							}
						});
					}else{
						$_this.unbind("click");
						$_this.bind("click",function(){
							if(DZQCheckForUser())
								return;
							
							var id = $_this.attr("id");
							var ids = id.split("-");
							var text = $_this.text();
							if(text.indexOf("取消") >= 0 ){
								Follow.followOrUnFollowUser(ids[1], -1,this);
							}
							else{
								Follow.followOrUnFollowUser(ids[1], 1,this);
							}
						});
					}
				});
				
			}
			
			if(questionReg && questionReg.length > 1){
				$(questionReg).unbind("click");
				$(questionReg).bind("click",function(){
					if(DZQCheckForUser())
						return;
					
					var id = $(this).attr("id");
					var ids = id.split("-");
					var text = $(this).text();
					if(text.indexOf("取消") >= 0 ){
						Follow.followOrUnFollowQuestion(ids[1], -1,this);
					}
					else{
						Follow.followOrUnFollowQuestion(ids[1], 1,this);
					}
				});
			}
		},
		/**
		 * 关注tag
		 * @param tag
		 * @param type
		 *            1,赞成-1,反对
		 */
		followOrUnFollowTag : function (tag, type,trigger) {
			if(DZQCheckForUser()||isSubmit)
				return;
			if(!checkAuth(DZQ_ACTIONS.FOLLOW)){
				return;
			}
			
			Wenda.post(Constans.BASE_PATH+"json/userJsonAction!followOrUnFollowTag.action", {
				"followedByTag.tag" : tag,
				"followType" : type
			}, function(data) {
				if (data.isLogin == 'no') {
					window.location.href = "/wenda/login.jsp";
					return;
				}
				if (type == '1') {// 关注
					if (data.followType > 0) {
						Follow.changeFollowText(trigger,-1);
					} else {
						alert("您已经关注了该TAG");
					}
				} else {// 取消关注
					if (data.followType > 0) {
						Follow.changeFollowText(trigger,1);
					} else {
						alert("您还没有关注该TAG");
					}
				}
			}, 'json');
		},
		/**
		 * 关注问题
		 * @param qid
		 * @param type
		 *            1,赞成-1,反对
		 */
		followOrUnFollowQuestion : function (qid, type,trigger) {
			if(DZQCheckForUser()||isSubmit)
				return;
			if(!checkAuth(DZQ_ACTIONS.FOLLOW)){
				return;
			}
			
			Wenda.post(Constans.BASE_PATH+"json/userJsonAction!followOrUnFollowQuestion.action", {
				"followedByQuestion.qid" : qid,
				"followType" : type
			}, function(data) {
				if (data.isLogin == 'no') {
					window.location.href = "/wenda/login.jsp";
					return;
				}
				if (type == '1') {// 关注
					if (data.followType > 0) {
						Follow.changeFollowText(trigger,-1);
					} else {
						Wenda.alert("您已经关注了该问题");
					}
				} else {// 取消关注
					if (data.followType > 0) {
						Follow.changeFollowText(trigger,1);
					} else {
						Wenda.alert("您还没有关注该问题");
					}
				}
			}, 'json');
		},
		/**
		 * 关注用户
		 * @param uid
		 * @param type
		 *            1,赞成-1,反对
		 */
		followOrUnFollowUser : function (uid, type,trigger) {
			
			if(DZQCheckForUser()||isSubmit)
				return;
			
			if(User.id == uid){
				// alert("你不能关注自己");
				Wenda.alert("你不能关注自己");
				return;
			}
			
			if(!checkAuth(DZQ_ACTIONS.FOLLOW)){
				return;
			}
			
			Wenda.post(Constans.BASE_PATH+"json/userJsonAction!followOrUnFollowUser.action", {
				"followedByUser.destuid" : uid,
				"followType" : type
			}, function(data) {
				if (data.isLogin == 'no') {
					window.location.href = Constans.BASE_PATH+"login.jsp";
					return;
				}
				if (type == '1') {// 关注
					if (data.followType > 0) {
						Follow.changeFollowText(trigger,-1);
					} else if (data.followType == '-2'){
						Wenda.alert("您要关注的用户不存在");
					} else {
						Wenda.alert("您已经关注了该用户");
					}
				} else {// 取消关注
					if (data.followType > 0) {
						Follow.changeFollowText(trigger,1);
					} else if (data.followType == '-2'){
						Wenda.alert("您要取消关注的用户不存在");
					}  else {
						Wenda.alert("您还没有关注该用户");
					}
				}
			}, 'json');
		},
		/**
		 * 切换文字显示
		 * @param trigger
		 * @param type
		 */
		changeFollowText : function (trigger,type){
			var $trigger = $(trigger); 
			var $p = $trigger.parent();
			if($trigger.attr("data-name") == "popup"){
				if(type == -1){
					$("#del_follow").show();
					$("#add_follow").hide();
				}else{
					$("#del_follow").hide();
					$("#add_follow").show();
				}
			}else
			if($trigger.attr("name") == "focus"){
				if(type == -1){
					$trigger.text("取消关注").removeClass().addClass("xiw xgw");
				}else{
					$trigger.text("关注").removeClass().addClass("xiw xhw");
				}
			}
			else if($p.attr("class") && $p.attr("class").indexOf("b_add_attention") >= 0){//按钮形式
				$p.removeClass("b_add_attention").addClass("b_attented");
				//$trigger.parent().html('<a href="javascript:;" id="'+$trigger.attr("id")+'">取消关注</a>');
				
				$p.html(($p.attr("fr")==1?'互相关注 ':'已关注')+'<span class="b_line">|</span><a href="javascript:;" class="b_blue" id="'+$trigger.attr("id")+'">取消</a>');
				Follow.followButtonEvent();
			}else if($p.attr("class") && $p.attr("class").indexOf("b_attented") >= 0){//按钮形式
				$p.removeClass("b_attented");
				$p.addClass("b_add_attention");
				$p.html('<a href="javascript:;" id="'+$trigger.attr("id")+'">加关注</a>');
				Follow.followButtonEvent();
			}else if($p.attr("class") && $p.attr("class").indexOf("grey_btn") >= 0){//按钮形式
				$p.removeClass("grey_btn");
				$p.addClass("green_btn");
				$p.html('<a href="javascript:;" id="'+$trigger.attr("id")+'">立即关注</a>');
				Follow.followButtonEvent();
			}else if($p.attr("class") && $p.attr("class").indexOf("green_btn") >= 0){
				$p.removeClass("green_btn");
				$p.addClass("grey_btn");
				$p.html('已关注<span>|</span><a href="javascript:;" id="'+$trigger.attr("id")+'">取消</a>');
				Follow.followButtonEvent();
			}else{
				if(type == -1){
					$trigger.text("- 取消关注");
					//$trigger.attr("class","grey_btn");
					
				}else{
					$trigger.text("+ 立即关注");
					//$trigger.attr("class","green_btn");
				}
			}
		}
	};
